package gcb.metalsys.controller.cop;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import gcb.metalsys.base.ResultUtil;
import gcb.metalsys.base.dto.ResultDTO;
import gcb.metalsys.entity.cop.ExtAccountInformation;
import gcb.metalsys.service.cop.ExtAccountInformationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @since 2020-07-15
 */

@Api(value = "表外账管理页面", tags = "表外账管理页面")
@RestController
@RequestMapping("/extAccountInformation")
public class ExtAccountInformationController {

    @Autowired
    private ExtAccountInformationService extAccountInformationService;

    @PostMapping
    @ApiOperation(value = "插入", notes = "插入")
    public ResultDTO insertOne(@RequestBody ExtAccountInformation extAccountInformation) throws Exception {
        extAccountInformationService.save(extAccountInformation);
        return ResultUtil.Success(extAccountInformation);
    }

    @PutMapping
    @ApiOperation(value = "按ID修改",notes = "按ID修改")
    public ResultDTO updateById(@RequestBody ExtAccountInformation extAccountInformation) throws Exception {
        extAccountInformationService.updateById(extAccountInformation);
        return ResultUtil.Success();
    }

    @DeleteMapping("/{id:\\d+}")
    @ApiOperation("按ID删除")
    public ResultDTO deleteById(@PathVariable Long id) throws Exception {
        extAccountInformationService.removeById((long) id);
        return ResultUtil.Success();
    }

    @GetMapping()
    @ApiOperation("查询、分页返回")
    public ResultDTO selectEntityPage(String countNumber,
                                            @RequestParam(value = "page", defaultValue = "0") int pageNum,
                                            @RequestParam(value = "limit", defaultValue = "8") int pageSize)throws Exception{

        QueryWrapper<ExtAccountInformation> extAccountInformationQueryWrapper = new QueryWrapper<>();

        extAccountInformationQueryWrapper.like("count_number",countNumber);
        IPage<ExtAccountInformation> page = extAccountInformationService.page(new Page<ExtAccountInformation>(pageNum, pageSize), extAccountInformationQueryWrapper);
        List<ExtAccountInformation> data = page.getRecords();
        int count = (int) page.getTotal();
        return ResultUtil.Success(data,count);
    }


    @GetMapping("/selectEntityPage")
    @ApiOperation(value="表外账（分页）", notes="表外账（分页）")
    public ResultDTO selectPageList( String countNumber,
                                      @RequestParam(value = "page", defaultValue = "1") int pageNum,
                                      @RequestParam(value = "limit", defaultValue = "8") int pageSize)throws Exception{


        List<Map<String, Object>> extAccountInformationList = extAccountInformationService.listPage(countNumber,(pageNum-1)*pageSize,pageSize);
        QueryWrapper<ExtAccountInformation> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("count_number",countNumber);
        int extAccountInformationTotal = extAccountInformationService.list(queryWrapper).size();


        return ResultUtil.Success(extAccountInformationList,extAccountInformationTotal);
    }


    @GetMapping("/{id:\\d+}")
    @ApiOperation("ID查询")
    public ResultDTO selectEntityPage(@PathVariable Long id)throws Exception{
        return ResultUtil.Success(extAccountInformationService.getById(id));
    }

}
